import os
from time import sleep

import allure

import page
from page.page_glsx import PageGlsx
from page.page_jiamingceshi import PageJiamingceshi
from page.page_login import PageLogin
from tool.get_driver import GetDriver
from tool.get_log import GetLogger

log = GetLogger.get_log()
module_name = str(os.path.basename(__file__)).split('.')[0]

@allure.epic("五十音页面")
@allure.feature("wsy广告位测试用例")
class TestWsyAd:
    num = 0
    @classmethod
    def setup_class(cls):
        cls.driver = GetDriver.get_app_driver()
        cls.login = PageLogin(cls.driver)
        cls.wsy = PageJiamingceshi(cls.driver)
        cls.glsx = PageGlsx(cls.driver)
        try:
            cls.login.base_click_element(page.approve)
        except:
            pass
        try:
            cls.login.base_click_element(page.skip_user_desc)
        except:
            pass
        # cls.login.pagelogin(jiamingceshi_username, '123456')
        # cls.login.base_click_element(page.wsy_tab)
        try:
            cls.login.pagelogin_close_login()
        except:
            pass
        cls.login.pagelogin_close_first_page_tanchuang()

    @classmethod
    def teardown_class(cls):
        GetDriver.quit_app_driver()

    @classmethod
    def setup_method(cls):
        cls.login.pagelogin_click_mine()
        username = os.environ['WSY_AD_USERNAME_AUTO'] + str(cls.num)
        # username = '1110000218' + str(cls.num)
        cls.login.pagelogin(username, password='123456')
        if cls.num == 9:
            cls.num = 0
        else:
            cls.num += 1

    @classmethod
    def teardown_method(cls):
        cls.login.pagelogin_click_mine()
        cls.login.pagelogin_logout()

    @allure.story("五十音图banner测试用例")
    def test01_wsyad_banner(self):
        self.wsy.pagejiamingceshi_click_wsy_tab()
        self.wsy.pagewsy_click_banner()
        class_type = self.glsx.pageglsx_get_class_type()
        try:
            sleep(3)
            assert self.glsx.pageglsx_logic_selected(class_type)
            log.info('断言成功')
            self.glsx.base_get_screenshot_as_file(module_name, '断言成功截图')
        except Exception:
            self.glsx.base_get_screenshot_as_file(module_name, '跟老师学弹窗失败')
            log.error('断言失败')
            raise
        finally:
            self.wsy.pagejiamingceshi_return_to_wsypage_keycode()

    @allure.story("假名测试成绩单广告位")
    def test02_wsyad_report_ad(self):
        self.wsy.pagejiamingceshi_click_wsy_tab()
        self.wsy.pagejiamingceshi_click_start_study()
        self.wsy.pagejiamingceshi_click_ceshi()
        # 应用正式站逻辑（非全部选对）
        while True:
            try:
                self.wsy.pagejiamingceshi_click_fixed_answer()
            except:
                pass
            if self.wsy.pagejiamingceshi_if_confirm_exist():
                self.wsy.pagejiamingceshi_click_confirm()
                sleep(1)
                if self.wsy.pagejiamingceshi_if_next_question_exist():
                    self.wsy.pagejiamingceshi_click_next_question()
            if self.wsy.pagejiamingceshi_if_question_exist():
                pass
            else:
                break
        sleep(2)
        self.wsy.pagewsy_click_report_ad()
        class_type = self.glsx.pageglsx_get_class_type()
        try:
            sleep(3)
            assert self.glsx.pageglsx_logic_selected(class_type)
            log.info('断言成功')
            self.glsx.base_get_screenshot_as_file(module_name, '断言成功截图')
        except Exception:
            self.glsx.base_get_screenshot_as_file(module_name, '跟老师学弹窗失败')
            log.error('断言失败')
            raise
        finally:
            self.wsy.pagejiamingceshi_return_to_wsypage_keycode()

    # @allure.story("阶段测试广告位")
    # def test03_wsyad_report_ad(self):
    #     self.wsy.pagejiamingceshi_click_start_study()
    #     self.wsy.pagejiamingceshi_click_ceshi()
    #     # 应用正式站逻辑（非全部选对）
    #     while True:
    #         try:
    #             self.wsy.pagejiamingceshi_click_fixed_answer()
    #         except:
    #             pass
    #         if self.wsy.pagejiamingceshi_if_confirm_exist():
    #             self.wsy.pagejiamingceshi_click_confirm()
    #             sleep(1)
    #             if self.wsy.pagejiamingceshi_if_next_question_exist():
    #                 self.wsy.pagejiamingceshi_click_next_question()
    #         if self.wsy.pagejiamingceshi_if_question_exist():
    #             pass
    #         else:
    #             break
    #     sleep(2)
    #     self.wsy.pagewsy_click_report_ad()
    #     class_type = self.glsx.pageglsx_get_class_type()
    #     try:
    #         sleep(3)
    #         assert self.glsx.pageglsx_logic_selected(class_type)
    #         log.info('断言成功')
    #         self.glsx.base_get_screenshot_as_file(module_name, '断言成功截图')
    #     except Exception:
    #         self.glsx.base_get_screenshot_as_file(module_name, '跟老师学弹窗失败')
    #         log.error('断言失败')
    #         raise
    #     finally:
    #         self.wsy.pagejiamingceshi_return_to_wsypage_keycode()
